const path = require('path')
const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const FileManagerPlugin = require('filemanager-webpack-plugin')
const autoprefixer = require('autoprefixer')
const manifest = require('./vendor-manifest.json')

const postcssOpts = {
    ident: 'postcss',
    plugins: () => [
        autoprefixer({
            browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
        }),
    ]
}

const config = {
    mode: 'production',
    devtool: 'source-map',
    entry: {
        admin: path.resolve(__dirname, 'src/admin'),
        index: path.resolve(__dirname, 'src/index'),
        console: path.resolve(__dirname, 'src/console'),
        sign: path.resolve(__dirname, 'src/sign')
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'js/[name].js'
    },
    resolve: {
        modules: [path.resolve(__dirname, 'node_modules'), path.join(__dirname, 'src')],
        extensions: ['.web.js', '.jsx', '.js', '.json'],
    },
    module: {
        rules: [{
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            options: {
                plugins: [
                    "transform-decorators-legacy",
                    ["transform-runtime", { polyfill: false }],
                    ["import", [{
                        "style": "css",
                        "libraryName": "antd-mobile"
                    }, {
                        "libraryName": "antd",
                        "libraryDirectory": "es",
                        "style": "css"
                    }]]
                ],
                presets: ['env', 'react', 'stage-0']
            }
        },{
            test: /\.(jpg|png)$/,
            loader: "url-loader?limit=8192"
        },{
            test: /\.less$/i,
            use: ExtractTextPlugin.extract({
                fallback: 'style-loader',
                use:['css-loader','less-loader'],
            })
        },{
            test: /\.css$/i,
            use: ExtractTextPlugin.extract({
                fallback: 'style-loader',
                use:['css-loader'],
            })
        },{
            test: /\.(ttf|woff|eot|svg)$/,
            use: [{
                loader: 'file-loader',
                options: {
                    name: 'style/css/[hash].[ext]',
                    //outputPath: 'style/css/',
                    publicPath: '../../'
                }
            }]
        }]
    },
    externals: {
        //"react": "React",
        "jquery": "jQuery"
    },
    optimization: {
        minimize: true,
        splitChunks: {
            chunks: 'all',
            name: 'common',
        },
    },
    performance : {
        hints : false
    },
    plugins: [
        new webpack.optimize.ModuleConcatenationPlugin(),
        new webpack.DllReferencePlugin({
            manifest,
            context: __dirname,
        }),
        new ExtractTextPlugin({ filename: 'style/css/[name].css', allChunks: true }),
        new FileManagerPlugin({
            onEnd: {
                copy: [{
                    source: path.resolve(__dirname, 'dist/**/*'),
                    destination: path.resolve(__dirname, '../src/main/resources/static/dist/')
                },{
                    source: path.resolve(__dirname, 'dist/**/*'),
                    destination: path.resolve(__dirname, '../out/production/classes/static/dist/')
                },{
                    source: path.resolve(__dirname, 'style/img/**/*'),
                    destination: path.resolve(__dirname, '../out/production/classes/static/style/img/')
                },{
                    source: path.resolve(__dirname, 'style/img/**/*'),
                    destination: path.resolve(__dirname, '../src/main/resources/static/style/img/')
                }]
            }
        }),
    ]
}

module.exports = config
